class Solution:
    def minimumDeletions(self, s: str) -> int:
        a = 0
        b = 0
        for i in range(len(s)):
            if s[i] == 'a':
                a += 1
            else:
                b += 1
        ans = a + b
        curA = 0
        curB = 0
        for i in range(len(s)):
            if s[i] == 'a':
                curA += 1
            else:
                curB += 1
            afterA = a - curA
            afterB = curB - 1 if s[i] == 'b' else curB
            ans = min(ans, afterA + afterB)
        return ans
